/**
 * 
 */
package com.bestbuy.daoimple;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bestbuy.dao.ProductDao;
import com.bestbuy.pojo.ImageProduct;
import com.bestbuy.pojo.Product;
import com.bestbuy.pojo.ProductDetail;

/**
 * @author Tuan
 * 
 */
@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ProductDaoImpl implements ProductDao {

	@Autowired
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.ProductDao#getListProduct()
	 */
	public List<Product> getListProduct() {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Product where";
		Query query = session.createQuery(sql);
		List<Product> products = (List<Product>) query.list();

		
		return products;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.ProductDao#getProduct()
	 */

	public Product getProduct(Integer idproduct) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Product pr where pr.idproduct =:idproduct";
		Query query = session.createQuery(sql);
		query.setString("idproduct", idproduct.toString());
		Product product = (Product)query.uniqueResult();

		
		return product;
	}
	public Product getProduct(String idProduct) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Product where idproduct = ?";
		Query query = session.createQuery(hql);
		query.setString(0, idProduct);
		return (Product)query.uniqueResult();
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#Search(java.lang.String, java.lang.String)
	 */
	public List<Product> Search(String keySearch, String idCatagory,String Page) {
		Session session = sessionFactory.getCurrentSession();
		int count = this.countProduct(keySearch,idCatagory);
		int page = Integer.parseInt(Page)-1;
		List<Product> products ;
		if(keySearch == "")
		{
			if(idCatagory.equals("0"))
			{
				String sql = "from Product";
				Query query = session.createQuery(sql);
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
			else
			{
				String sql = "from Product pr where pr.catalog.idcatalog=:idCatagory";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
		}
		else
		{
			if(idCatagory.equals("0"))
			{
				String sql = "from Product pr where pr.productname like :keySearch";
				Query query = session.createQuery(sql);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
			else
			{
				String sql = "from Product pr where pr.catalog.idcatalog=:idCatagory and pr.productname like :keySearch";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
		}
		
		
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#SearchByPrice(java.lang.String, java.lang.String)
	 */
	public List<Product> SearchByPrice(String startPrice, String endPrice) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Product pr where ";
		Query query = session.createQuery(sql);
		
		List<Product> products = (List<Product>) query.list();
		return products;
		
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#countProduct()
	 */
	public int countProduct(String keySearch, String idCatagory) {
		Session session = sessionFactory.getCurrentSession();
		if(keySearch == "")
		{
			if(idCatagory.equals("0"))
			{
				String sql = "select count(*) from Product";
				Query query = session.createQuery(sql);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
			else
			{
				String sql = "select count(*) from Product pr where pr.catalog.idcatalog=:idCatagory";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
		}
		else
		{
			if(idCatagory.equals("0"))
			{
				String sql = "select count(*) from Product pr where pr.productname like :keySearch";
				Query query = session.createQuery(sql);
				query.setString("keySearch", "%"+keySearch+"%");
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
			else
			{
				String sql = "select count(*) from Product pr where pr.catalog.idcatalog=:idCatagory and pr.productname like :keySearch";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("keySearch", "%"+keySearch+"%");
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#updateProduct(com.bestbuy.pojo.Product)
	 */
	public void updateProduct(Product product) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.update(product);
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#saveProduct(com.bestbuy.pojo.Product)
	 */
	public void saveProduct(Product product) {
		Session session = sessionFactory.getCurrentSession();
		try {
			session.save(product);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#saveProductDetail(com.bestbuy.pojo.ProductDetail)
	 */
	public void saveProductDetail(ProductDetail detail) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(detail);
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#getProductByName(java.lang.String)
	 */
	public Product getProductByName(String nameProduct) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Product where productname = ?";
		Query query = session.createQuery(hql);
		query.setString(0, nameProduct);
		return (Product)query.uniqueResult();
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#getListProduct(java.lang.String, java.lang.String)
	 */
	public List<Product> getListProduct(String page, String isDelete) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Product where isdeleted = ?";
		int temp = Integer.parseInt(page)-1;
		List<Product> products ;
		Query query = session.createQuery(sql);
		query.setFirstResult(temp*5);
		query.setMaxResults(5);
		products = (List<Product>) query.list();
		return products;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#countProduct(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int countProduct(String keySearch, String idCatagory, String isDelete) {
		Session session = sessionFactory.getCurrentSession();
		if(keySearch == "")
		{
			if(idCatagory.equals("0"))
			{
				String sql = "select count(*) from Product where isdeleted = ?";
				Query query = session.createQuery(sql);
				query.setString(0, isDelete);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
			else
			{
				String sql = "select count(*) from Product pr where pr.catalog.idcatalog=:idCatagory and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("isdeleted", isDelete);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
		}
		else
		{
			if(idCatagory.equals("0"))
			{
				String sql = "select count(*) from Product pr where pr.productname like :keySearch and and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setString("isdeleted", isDelete);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
			else
			{
				String sql = "select count(*) from Product pr where pr.catalog.idcatalog=:idCatagory and pr.productname like :keySearch and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setString("isdeleted", isDelete);
				int count = ((Long) query.uniqueResult()).intValue();
				return count;
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.ProductDao#Search(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public List<Product> Search(String keySearch, String idCatagory,
			String Page, String isDelete) {
		Session session = sessionFactory.getCurrentSession();
		int count = this.countProduct(keySearch,idCatagory);
		int page = Integer.parseInt(Page)-1;
		List<Product> products ;
		if(keySearch == "")
		{
			if(idCatagory.equals("0"))
			{
				String sql = "from Product where isdeleted = ?";
				Query query = session.createQuery(sql);
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				query.setString(0, isDelete);
				products = (List<Product>) query.list();
				return products;
			}
			else
			{
				String sql = "from Product pr where pr.catalog.idcatalog=:idCatagory and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("isdeleted", isDelete);
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
		}
		else
		{
			if(idCatagory.equals("0"))
			{
				String sql = "from Product pr where pr.productname like :keySearch and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("isdeleted", isDelete);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
			else
			{
				String sql = "from Product pr where pr.catalog.idcatalog=:idCatagory and pr.productname like :keySearch and pr.isdeleted=:isdeleted";
				Query query = session.createQuery(sql);
				query.setString("idCatagory", idCatagory);
				query.setString("keySearch", "%"+keySearch+"%");
				query.setString("isdeleted", isDelete);
				query.setFirstResult(page*5);
				query.setMaxResults(5);
				products = (List<Product>) query.list();
				return products;
			}
		}
	}


}
